package com.one.group.admin.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.convert.Convert;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.one.group.enums.ErrorCode;
import com.one.group.model.dto.BaseDeleteDto;
import com.one.group.model.dto.UpdateRepairReportDto;
import com.one.group.model.dto.RepairReportQueryDto;
import com.one.group.model.dto.UpdateRepairReportStatusDto;
import com.one.group.model.entity.RepairReport;
import com.one.group.model.vo.RepairReportListVo;
import com.one.group.result.Result;
import com.one.group.service.RepairReportService;
import com.one.group.utils.ResultUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/repairReport")
@Tag(name = "报事报修管理接口")
@Slf4j
public class RepairReportController {
    @Autowired
    RepairReportService RepairReportService;
    @PostMapping("list")
    @Operation(summary = "查询报事报修列表")
    public Result list(@RequestBody RepairReportQueryDto repairReportQueryDto){
//        1.准备分页条件
        PageHelper.startPage(repairReportQueryDto.getPage(),repairReportQueryDto.getLimit());
//        2.链式编程准备查询条件

        List<RepairReportListVo> RepairReportListVos = RepairReportService.findRepairReportByQueryDto(repairReportQueryDto);

//        3.获取分页信息
        PageInfo<RepairReportListVo> pageInfo =new PageInfo<>(RepairReportListVos);
//        4.返回分页数据
        return ResultUtil.success(pageInfo);
    }

    @GetMapping("/find")
    @Operation(summary = "根据id查询报事报修信息")
    public Result find(@RequestParam Long id){
        return ResultUtil.success(RepairReportService.getById(id));
    }
    @GetMapping("findAll")
    @Operation(summary = "查询所有报事报修信息")
    public Result findAll() {
        return ResultUtil.success(RepairReportService.list());
    }
    @GetMapping("/delete")
    @Operation(summary = "删除报事报修信息")
    public Result delete(@RequestParam Long id){
        if (RepairReportService.removeById(id))
            return ResultUtil.success();
        else return ResultUtil.error(ErrorCode.OPERATION_ERROR);
    }
    @PostMapping("/add")
    @Operation(summary = "添加报事报修信息")
    public Result add(@RequestBody UpdateRepairReportDto updateRepairReportDto){
        RepairReport RepairReport = new RepairReport();
        BeanUtils.copyProperties(updateRepairReportDto,RepairReport);
        if (RepairReportService.save(RepairReport)) return ResultUtil.success();
        else return ResultUtil.error(ErrorCode.OPERATION_ERROR);
    }
    @PostMapping("/update")
    @Operation(summary = "修改报事报修信息")
    public Result update(@RequestBody UpdateRepairReportDto updateRepairReportDto){
//        RepairReport RepairReport = new RepairReport();
//        BeanUtils.copyProperties(updateRepairReportDto,RepairReport);
        log.debug("updateRepairReportDto:{}",updateRepairReportDto);
        if (RepairReportService.updateRepairReport(updateRepairReportDto))
            return ResultUtil.success();
        else return ResultUtil.error(ErrorCode.OPERATION_ERROR);
    }
    @PostMapping("/updateStatus")
    @Operation(summary = "修改状态信息")
    public Result updateStatus(@RequestBody UpdateRepairReportStatusDto updateRepairReportStatusDto){
        if (RepairReportService.updateStatus(updateRepairReportStatusDto))
            return ResultUtil.success();
        else return ResultUtil.error(ErrorCode.OPERATION_ERROR);
    }
    @PostMapping("/deleteBatch")
    @Operation(summary = "批量删除报事报修信息")
    public Result deleteBatch(@RequestBody BaseDeleteDto baseDeleteDto){
        if (RepairReportService.removeByIds(baseDeleteDto.getIds())) return ResultUtil.success();
        else return ResultUtil.error(ErrorCode.OPERATION_ERROR);
    }

}
